package com.example.androidcomplete02.parser;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import com.example.androidcomplete02.R;

public class Pull_parserAcitivity extends Activity {
	ListView list;
	ProgressDialog progressDlg;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch28_network_xmlparser);
		list = (ListView) findViewById(R.id.listView);
		new ParsingXml().execute("order.xml");
	}

	private class ParsingXml extends AsyncTask<String, Void, ArrayList<Item>> {
		protected void onPreExecute() {
			progressDlg = ProgressDialog.show(Pull_parserAcitivity.this, "Wait",
					"xml parsing ...");
		}

		protected void onPostExecute(ArrayList<Item> result) {
			MyAdapter adapter = new MyAdapter(Pull_parserAcitivity.this,result);
			progressDlg.dismiss();
			list.setAdapter(adapter);
		}

		protected ArrayList<Item> doInBackground(String... params) {
			StringBuilder sb = new StringBuilder();
			String str = null;
			
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						openFileInput(params[0])));
				while ((str = br.readLine()) != null) {
					sb.append(str);
				}
				br.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return parsingXml(sb.toString());
		}
	}

	public ArrayList<Item> parsingXml(String xml) {
		ArrayList<Item> arItem = new ArrayList<Item>();
		Item itemClass = null;
		boolean initItem = false;
		
		try {
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			XmlPullParser parser = factory.newPullParser();
			
			parser.setInput(new StringReader(xml));
			
			int eventType = parser.getEventType();
			while(eventType != XmlPullParser.END_DOCUMENT){
				switch(eventType){
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if(parser.getName().equalsIgnoreCase("item")){
						initItem = true;
						itemClass = new Item();
					}
					if(parser.getAttributeCount() > 0){
						for (int i = 0; i < parser.getAttributeCount(); i++) {
							if(parser.getAttributeName(i).equalsIgnoreCase("price")){
								itemClass.setItemPrice(Integer.parseInt(parser.getAttributeValue(i)));
							}else{
								itemClass.setMakerName(parser.getAttributeValue(i));
							}
						}
					}
					break;
				case XmlPullParser.TEXT:
					if(initItem){
						itemClass.setItemName(parser.getText());
						initItem = false;
					}
					break;
				case XmlPullParser.END_TAG:
					if(parser.getName().equalsIgnoreCase("item")){
						arItem.add(itemClass);
					}					
					break;
				}
				eventType = parser.next();
			}
		} catch (XmlPullParserException | IOException e) {
			e.printStackTrace();
		}
		
		
		return arItem;
	}
}